package com.hean.activity;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.RandomAccessFile;
import java.net.HttpURLConnection;
import java.net.URL;

public class Downloader {
	public void down() throws Exception {
		String urlpath="http://localhost:8080/dangdang/productImages/16.jpg";
		URL url=new URL(urlpath);
		HttpURLConnection connection=(HttpURLConnection) url.openConnection();
		connection.setRequestMethod("GET");
		int threadsize=3;
		int filesize=connection.getContentLength();
		int fileblock=filesize/3+1;
		connection.disconnect();
		File file=new File("1.jpg");
		RandomAccessFile accessFile=new RandomAccessFile(file, "rw");
		accessFile.setLength(filesize);
		for (int i = 0; i < threadsize; i++) {
			int startposition=i*fileblock;
			RandomAccessFile threadfile=new RandomAccessFile(file, "rw");
			new DownloadThread(i, url, startposition, threadfile, fileblock).start();
			Thread.sleep(100);
		}
		byte[] quit=new byte[1];
		System.in.read(quit);
		while('q'!=quit[0]){
			Thread.sleep(2000);
		}
	}
	
	private class DownloadThread extends Thread{
		private URL url;
		private int startposition;
		private RandomAccessFile accessFile;
		private int block;
		private int threadId;
		public DownloadThread(int threadId,URL url, int startposition,
				RandomAccessFile accessFile, int block) {
			this.threadId=threadId;
			this.url = url;
			this.startposition = startposition;
			this.accessFile = accessFile;
			this.block = block;
		}
		@Override
		public void run() {
			try {
				HttpURLConnection connection=(HttpURLConnection) url.openConnection();
				connection.setRequestMethod("GET");
				connection.setRequestProperty("Range", "bytes="+startposition+"-");
				accessFile.seek(startposition);
				connection.setConnectTimeout(6*1000);
				InputStream inputStream=connection.getInputStream();
				byte[] buffer=new byte[1024];
				int len=-1;
				int readfilesize=0;
				while(readfilesize<block&&(len=inputStream.read(buffer))!=-1){
					accessFile.write(buffer, 0, len);
					readfilesize+=len;
				}
				inputStream.close();
				connection.disconnect();
				System.out.println(threadId+" downloaded over");
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		
	}
	public void getImage() throws Exception{
		String urlpath="";
		URL url=new URL(urlpath);
		HttpURLConnection connection=(HttpURLConnection) url.openConnection();
		connection.setRequestMethod("get");
		connection.setConnectTimeout(6*1000);
		connection.setRequestProperty("RANGE", "bytes="+1024+"-");
		System.out.println(connection.getResponseCode());
		if(connection.getResponseCode()==200){//connection.setRequestProperty("RANGE",则responsecode是206
			InputStream inputStream=connection.getInputStream();
			byte[] data= readStream(inputStream);
			File file=new File("xxx.jpg");
			FileOutputStream outputStream=new FileOutputStream(file);
			outputStream.write(data);
			outputStream.close();
			
		}
	}
	public byte[] readStream(InputStream inputStream)throws Exception{
		ByteArrayOutputStream outputStream=new ByteArrayOutputStream();
		byte[] buffer=new byte[1024];
		int len=-1;
		while((len=inputStream.read(buffer))!=-1){
			outputStream.write(buffer,0,len);
		}
		outputStream.close();
		inputStream.close();
		return outputStream.toByteArray();
		
		
	}
}
